========
========
IMPORTANT: CURRENTLY ONLY TNHA USES WIND SPEED IN PCA. WHEN WE HAVE THE REST OF THE WIND DATA, UPDATE THIS SCRIPT AT LINE 188!!
filepath = "https://raw.githubusercontent.com/shabanm2/Utqiagvik/main/Meteorological_Seasons_Data/"
years = c("2022", "2023", "2024")
seasons = c("Spring", "Summer","Fall")
sites = c("TNHA", "BEO", "SSMH")
# put season values for the season that has the start of the data
date_start = "2022-06-01" # data starts in June 2022 (YEAR-MO-DY) where day is always 01
# put season values for the season that has the last of the data
date_end = "2024-10-01" # data ends after November of 2023 (will get data up UNTIL date_end not after)
scree = F # scree plot
eigen = T # eigenvectors and eigenvalues
spring_months = c("March", "April", "May")
summer_months = c("June","July","August")
fall_months = c("September", "October", "November")
winter_months = c("December", "January", "February")
spring_dates = data.frame(months=spring_months, start=c("-03-01","-04-01","-05-01"), end=c("-04-01","-05-01","-06-01"))
summer_dates = data.frame(months=summer_months, start=c("-06-01","-07-01","-08-01"), end=c("-07-01","-08-01","-09-01"))
fall_dates = data.frame(months=fall_months, start=c("-09-01","-10-01","-11-01"), end=c("-10-01","-11-01","-12-01"))
winter_dates = data.frame(months=winter_months, start=c("-12-01","-01-01","-02-01"), end=c("-01-01","-02-01","-03-01"))
all_dates = data.frame(matrix(nrow = 0, ncol = 4))
for(yur in years){
# spring
if("Spring" %in% seasons){
for(i in c(1:3)){
curdate = as.Date(paste0(yur, spring_dates[i, 2]))
if(curdate >= date_start && curdate < date_end){
all_dates <- rbind(all_dates, c(spring_dates[i,1], paste0(yur, spring_dates[i, 2]), paste0(yur, spring_dates[i, 3]),"Spring"))
}
}
}
if("Summer" %in% seasons){
for(i in c(1:3)){
curdate = as.Date(paste0(yur, summer_dates[i, 2]))
if(curdate >= date_start && curdate < date_end){
all_dates <- rbind(all_dates, c(summer_dates[i,1], paste0(yur, summer_dates[i, 2]), paste0(yur, summer_dates[i, 3]),"Summer"))
}
}
}
if("Fall" %in% seasons){
for(i in c(1:3)){
curdate = as.Date(paste0(yur, fall_dates[i, 2]))
if(curdate >= date_start && curdate < date_end){
all_dates <- rbind(all_dates, c(fall_dates[i,1], paste0(yur, fall_dates[i, 2]), paste0(yur, fall_dates[i, 3]),"Fall"))
}
}
}
if("Winter" %in% seasons){
for(i in c(1:3)){
curdate = as.Date(paste0(yur, winter_dates[i, 2]))
if(curdate >= date_start && curdate < date_end){
all_dates <- rbind(all_dates, c(winter_dates[i,1], paste0(yur, winter_dates[i, 2]), paste0(yur, winter_dates[i, 3]),"Winter"))
}
}
}
}
colnames(all_dates) = c("months","start","end","szn")
========
========
library(dplyr)
library(lubridate)
library(tidyverse)
file <- "all_sites_daily_gap_filled.csv"
df <- read.csv(paste0(filepath, file))
# rename all BEO stations to BEO-BASE
df$station <- recode(df$station, "B05" = "BASE", "B06" = "BASE", "B07" = "BASE", "B08" = "BASE")
df$fullname <- recode(df$fullname, "BEO-B05" = "BEO-BASE", "BEO-B06" = "BEO-BASE", "BEO-B07" = "BEO-BASE", "BEO-B08" = "BEO-BASE")
# update depths
df$grounddepth <- recode(df$grounddepth, "7" = "3.5cm", "8" = "10cm", "9" = "20cm", "10" = "30cm", "11" = "40cm", "12" = "50cm", "13" = "55cm", "14" = "65cm", "15" = "75cm", "16" = "85cm", "17" = "90cm")
df$grounddepth = factor(df$grounddepth, c("3.5cm","10cm", "20cm", "30cm", "40cm", "50cm", "55cm", "65cm", "75cm", "85cm", "90cm"))
df$vwcdepth <- recode(df$vwcdepth, "1" = "0:15cm", "2" = "15:30cm", "3" = "30:45cm", "4" = "45:60cm", "5" = "60:75cm", "6" = "75:90cm")
df$vwcdepth = factor(df$vwcdepth, c("0:15cm","15:30cm", "30:45cm", "45:60cm", "60:75cm", "75:90cm"))
df$Date = as.character(df$day)
=========================
=========================
Temporal Range: Season Vertical Spatial Range: 30-45 cm Horizontal Spatial Range: stations across site (TNHA, SSMH, BEO) –> Average Total Site –> North vs South (except for BEO)
pick_site <- function(cursite){
# PREVIOUS CODE JOINED MULTIPLE DATAFRAMES (from PCA-S24.Rmd)
# gtfile = paste0("GroundTemperature_",szn,yr,"_DAILY.csv")
# airfile = paste0("AirTemperature_",szn,yr,"_DAILY.csv")
# vwcfile = paste0("VWC_",szn,yr,"_DAILY.csv")
# solfile = paste0("Solar_",szn,yr,"_DAILY.csv")
# windspeed = paste0("WindSpeed_",szn,yr,"_DAILY.csv")
# winddir = paste0("WindDirection_",szn,yr,"_DAILY.csv")
#
# grndtmp <<- read.csv(paste0(filepath, gtfile))
# airtmp <<- read.csv(paste0(filepath, airfile))
# vwc <<- read.csv(paste0(filepath, vwcfile))
# solar <<- read.csv(paste0(filepath, solfile))
# wind <<- read.csv(paste0(filepath, windspeed))
# pca_ground = grndtmp %>% filter(site == cursite) %>% filter(depth == "30cm")
# pca_air = airtmp %>% filter(site == cursite)
# pca_wind = wind %>% filter(site == cursite)
# pca_solar = solar %>% filter(site == cursite)
# pca_vwc = vwc %>% filter(site==cursite) %>% filter(depth == "30-45cm")
#
# big_df <<- full_join(pca_ground, pca_air, by=c("Date", "site", "station", "fullname")) %>%
# select(Date, site, station, fullname, avg.x, avg.y) %>% rename("groundtemp"="avg.x", "airtemp"="avg.y")
# big_df <<- full_join(big_df, pca_wind, by=c("Date", "site", "station", "fullname")) %>%
# select(Date, site, station, fullname, groundtemp, airtemp, avg) %>% rename("windspeed" = avg)
# big_df <<- full_join(big_df, pca_solar, by=c("Date", "site", "station", "fullname")) %>%
# select(Date, site, station, fullname, groundtemp, airtemp, windspeed, avg) %>% rename("solar" = avg)
# big_df <<- full_join(big_df, pca_vwc, by=c("Date", "site", "station", "fullname")) %>%
# select(Date, site, station, fullname, groundtemp, airtemp, windspeed, solar, avg) %>% rename("vwc" = avg)
big_df <<- df %>% filter(site == cursite) %>% filter(grounddepth == "30cm") %>% filter(vwcdepth == "30:45cm")
if (cursite == "TNHA"){
big_df <- big_df %>% select(Date, fullname, site, station, groundtemp, airtemp, windspeed, solar, vwc)
}
else {
big_df <- big_df %>% select(Date, fullname, site, station, groundtemp, airtemp, solar, vwc)
}
if(szn == "Winter"){
big_df <<- big_df %>% select(-solar)
}
return(big_df)
}
pick_dates <- function(datemin, datemax, big_df){
pca_df <<- big_df %>% filter(Date >= datemin) %>% filter(Date < datemax)
# get rid of NAs
pca_df <<- na.omit(pca_df)
pca_df <<- unique(pca_df)
return(pca_df)
}
calc_pca <- function(pca_df){
pca <<- prcomp(pca_df[,5:ncol(pca_df)], center=TRUE, scale.=TRUE)
#take out variables
sd <- pca$sdev
loads <<- pca$rotation
rownames(loads) <<- colnames(pca_df[5:ncol(pca_df)])
scores <<- pca$x
var <- sd^2
varPercent <- var/sum(var) * 100
return(list("pca"=pca, "loads"=loads))
}
make_scree <- function(pca){
sd <- pca$sdev
var <- sd^2
varPercent <- var/sum(var) * 100
barplot(varPercent, xlab="PC", ylab="Percent Variance", names.arg=1:length(varPercent),
las=1, ylim=c(0, max(varPercent)), col="gray")
abline(h=1/ncol(pca_df[5:ncol(pca_df)])*100, col="red")
}
make_eigen <- function(pca){
eigenvectors <- pca$rotation
print("Eigenvectors (Loadings):")
print(eigenvectors)
print("Loadings Cutoff:")
sqrt(1/ncol(pca_df[5:ncol(pca_df)])) # cutoff for "important" loadings
# Access the eigenvalues (variances of the principal components)
eigenvalues <- (pca$sdev)^2
print("Eigenvalues:")
print(eigenvalues)
}
===============
===============
make_pca <- function(pca_df, szn, yr, site){
if(site == "TNHA"){
SOUTH <<- pca_df$fullname == "TNHA-SA"
NORTH <<- pca_df$fullname == "TNHA-SC"
s <- "TNHA-SA"
n <- "TNHA-SC"
} else{
if(site == "SSMH"){
SOUTH <<- pca_df$fullname == "SSMH-SA"
NORTH <<- pca_df$fullname == "SSMH-SB"
s <- "SSMH-SA"
n <- "SSMH-SB"
} else {
SOUTH <<- pca_df$fullname == "BEO-BASE"
n <- "BEO"
}
}
scaling <- 2
textNudge <- 1.1
limNudge <- 1.3
xlimit <- seq(floor(min(scores[,1])*limNudge),ceiling(max(scores[,1])*limNudge), 1)
ylimit <- seq(floor(min(scores[,2])*limNudge),ceiling(max(scores[,2])*limNudge), 1)
plot(scores[, 1], scores[, 2], xlab="PCA 1", ylab="PCA 2", type="n", asp=1,
las=1, xaxt='n', yaxt='n')
axis(side = 1, at=xlimit)
axis(side = 2, at=ylimit)
title(paste0(szn, " ", yr," Principal Component Analysis:\n",site," North v. South (",format(as.Date(min(pca_df$Date)), format="%m/%d/%Y")," - ",format(as.Date(max(pca_df$Date)), format="%m/%d/%Y"), ")"), adj=0.5)
points(scores[SOUTH, 1], scores[SOUTH, 2], pch=16, cex=0.7, col="mediumturquoise")
if(site != "BEO"){
points(scores[NORTH, 1], scores[NORTH, 2], pch=16, cex=0.7, col="salmon")
legend(x = "topright", # Position
legend = c(paste0(s, " (south)"), paste0(n, " (north)")), # Legend texts
col = c("mediumturquoise","salmon"),
pch = 19) #colors
} else{
legend(x = "topright", # Position
legend = "BEO", # Legend texts
col = "mediumturquoise",
pch = 19)
}
arrows(0, 0, loads[, 1]* scaling, loads[, 2]* scaling, length=0.1, angle=20, col="darkred")
text(loads[1, 1]*scaling*textNudge, loads[1, 2]*scaling*textNudge, rownames(loads)[1], col="darkred", cex=0.7) # ground label
text(loads[2, 1]*scaling*textNudge, loads[2, 2]*scaling*textNudge+0.2, rownames(loads)[2], col="darkred", cex=0.7) # air label
if(nrow(loads) > 2){
text(loads[3, 1]*scaling*textNudge, loads[3, 2]*scaling*textNudge, rownames(loads)[3], col="darkred", cex=0.7) # wind label
if(nrow(loads)>3){
text(loads[4, 1]*scaling*textNudge-0.2, loads[4, 2]*scaling*textNudge, rownames(loads)[4], col="darkred", cex=0.7) # solar label
if(nrow(loads)>4){
text(loads[5, 1]*scaling*textNudge, loads[5, 2]*scaling*textNudge, rownames(loads)[5], col="darkred", cex=0.7) # vwc label
}
}
}
#text(-3, 1]*scaling*textNudge, 1, "TNHA-SA \n(south)", col="mediumturquoise")
#text(1, 1, "TNHA-SC \n(north)", col="salmon")
}
for(i in c(1:nrow(all_dates))){
month <- all_dates$months[i]
startdate <- all_dates$start[i]
enddate <- all_dates$end[i]
szn <<- all_dates$szn[i]
yr <<- substr(all_dates$start[i], 1, 4)
for(site in sites){
big_df <- pick_site(site)
pca_df <- pick_dates(startdate, enddate, big_df)
if(nrow(pca_df) > 4){
p <- calc_pca(pca_df)
pca <- p$pca
loads <- p$loads
if(scree == T){
make_scree(pca)
}
if(eigen == T){
make_eigen(pca)
}
make_pca(pca_df, szn, yr, site)
}
}
}
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp 0.58137611 0.29351723 -0.1493024 -0.2625688 0.69614359
## airtemp 0.40081787 0.02227347 0.8921022 0.1909985 -0.08075967
## windspeed 0.35933972 -0.58168040 -0.2763283 0.6616764 0.13546167
## solar -0.03043223 0.75339953 -0.1486000 0.6341684 -0.08492024
## vwc 0.60934039 0.08595687 -0.2888306 -0.2336492 -0.69519830
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.1866519 1.4752764 0.7964961 0.3521848 0.1893908
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp 0.4749785 0.6269735 -0.32162695 0.52712019
## airtemp 0.5544423 0.2745665 0.07185678 -0.78233213
## solar -0.5195360 0.2974824 -0.72949295 -0.33079690
## vwc -0.4439273 0.6655987 0.59936028 -0.02596449
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.7259363 0.7739135 0.3335526 0.1665976
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp 0.5573997 -0.4117315 -0.3114187 0.6502316
## airtemp 0.5437827 -0.2457990 0.7598607 -0.2578660
## solar -0.2468644 -0.8341033 -0.2394967 -0.4312436
## vwc 0.5767703 0.2726385 -0.5179492 -0.5698534
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.6994731 1.0926648 0.6872942 0.5205679
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp -0.4317808 -0.47061294 -0.36001139 -0.6188737 -0.2819147
## airtemp -0.4224374 -0.56445877 -0.04191413 0.5395834 0.4582859
## windspeed 0.3933992 -0.58981171 0.40694961 0.1856797 -0.5452286
## solar -0.5368509 0.32495475 -0.06199164 0.4511760 -0.6315004
## vwc 0.4383379 -0.08022666 -0.83617226 0.2963248 -0.1201295
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.2422577 1.2216905 0.7335001 0.5025504 0.3000013
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp 0.5003745 0.5950382 -0.25554178 -0.5746766
## airtemp 0.5624212 0.2551834 -0.06682613 0.7836442
## solar 0.4901746 -0.2571830 0.80867122 -0.1990896
## vwc -0.4393594 0.7174035 0.52562678 0.1265385
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.6226720 0.6936594 0.4832533 0.2004152
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.57484614 -0.2289384 0.57931534 0.5305967
## airtemp -0.47073897 0.4650109 -0.64043715 0.3898845
## solar -0.66921085 -0.1169531 -0.03912257 -0.7327675
## vwc 0.01065696 0.8471564 0.50269616 -0.1717818
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.8999944 1.1274065 0.7257056 0.2468935
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp 0.3617158 0.565296864 0.1501319 0.5624719 -0.4590064
## airtemp 0.3170337 0.573836961 -0.5492273 -0.2761577 0.4385055
## windspeed -0.4152141 0.489213591 0.2738220 -0.6075697 -0.3796681
## solar 0.5413681 -0.334370851 -0.2968998 -0.4086589 -0.5830653
## vwc -0.5506080 0.004097993 -0.7160181 0.2668691 -0.3360254
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.9334411 1.5230496 0.6571767 0.5561107 0.3302219
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp 0.5721966 -0.2561850 0.3214399 0.70967367
## airtemp 0.5708581 -0.1224752 0.4200583 -0.69474589
## solar 0.3815226 0.9127452 -0.1235919 0.07785699
## vwc -0.4485004 0.2937094 0.8396145 0.08734824
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.4996565 0.7513760 0.6118481 0.1371195
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.5535001 -0.1413396 -0.03736764 0.8199173
## airtemp -0.4526918 0.7351897 0.47949325 -0.1570109
## solar -0.4771209 -0.6534455 0.41529621 -0.4158048
## vwc 0.5109436 -0.1119295 0.77215214 0.3608178
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.2409442 0.7319197 0.5838249 0.4433112
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp -0.5685001 -0.06268284 0.3240193 0.3911702829 -0.6441086
## airtemp -0.4942970 0.40560349 0.4497682 0.0008752545 0.6235897
## windspeed 0.3946973 0.52459340 0.5035564 -0.4032156174 -0.3909782
## solar -0.5115169 -0.09488905 -0.2600369 -0.7985494931 -0.1550678
## vwc -0.1226485 0.73983411 -0.6095341 0.2161502179 -0.1391046
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.2377598 1.1488593 0.8279779 0.5365954 0.2488076
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.6119639 0.06522055 0.3736538 -0.69399518
## airtemp -0.5879580 0.34380955 0.2531106 0.68704834
## solar -0.4820289 -0.08038011 -0.8709435 -0.05142619
## vwc 0.2178388 0.93331683 -0.1943587 -0.20902299
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.35887157 0.99086761 0.58710337 0.06315745
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.5986082 -0.03491101 0.3173196 -0.7346820
## airtemp -0.5040039 0.49341187 0.4221027 0.5695210
## solar -0.5886010 -0.08886694 -0.7908358 0.1422331
## vwc 0.2029708 0.86453954 -0.3093805 -0.3400852
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.2245861 1.1235740 0.3424200 0.3094199
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp -0.4336096 -0.05329431 -0.808533863 -0.3531042 0.1753080
## airtemp -0.4638532 -0.46754740 0.138403799 0.5620022 0.4808716
## windspeed 0.3024553 -0.87407939 -0.096119098 -0.1789274 -0.3213287
## solar -0.5379080 0.04962938 -0.008708819 0.2788136 -0.7939641
## vwc -0.4647565 -0.10991493 0.563739883 -0.6706109 0.0663208
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.6243707 0.9237834 0.6673881 0.4635558 0.3209020
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.5288234 0.5035340 -0.2246075 0.645252462
## airtemp -0.5818232 0.1794905 -0.3168936 -0.727216142
## solar -0.4951732 -0.1246875 0.8597506 -0.009249606
## vwc -0.3696364 -0.8358766 -0.3316004 0.233923894
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.5528983 0.8463771 0.4882093 0.1125153
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.253548434 0.5403810 -0.79798471 -0.0831985
## airtemp -0.001032505 -0.8242561 -0.54013189 -0.1698775
## solar -0.689396504 -0.1660356 0.03317695 0.7043181
## vwc 0.678560672 0.0319755 -0.26528710 0.6842191
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.0065564 1.0999825 0.8630724 0.0303887
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp -0.5754511 0.28654325 0.09263654 0.48645658 0.5844036
## airtemp -0.2317870 0.77785622 0.14272761 -0.53457488 -0.1872779
## windspeed 0.1937843 0.26185817 -0.91620029 -0.02874316 0.2315790
## solar -0.4886383 -0.48412160 -0.14291257 -0.63985245 0.3114863
## vwc -0.5820748 -0.09944468 -0.33346714 0.25952336 -0.6875659
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.2364727 1.2583405 0.9746343 0.3191523 0.2114002
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.5826642 -0.3307725 -0.1831093 -0.71941854
## airtemp -0.5673770 -0.3881321 -0.2167550 0.69314797
## solar -0.3244785 0.7992550 -0.5053044 0.02393091
## vwc -0.4830109 0.3180165 0.8149575 0.03755226
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.56852683 0.95927661 0.45731267 0.01488389
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.01637029 0.70710682 -0.6818108 0.18672428
## airtemp -0.02162347 0.69791010 0.7150593 -0.03382415
## solar -0.70845170 0.05904997 -0.1119008 -0.69432521
## vwc 0.70523799 0.09713151 -0.1063126 -0.69419196
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.94906281 1.58127780 0.44758962 0.02206976
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp 0.08637424 -0.77275616 0.13042676 -0.5644290 -0.2445326
## airtemp -0.66395328 0.08664157 -0.03965365 -0.4758573 0.5688996
## windspeed 0.64983357 -0.13803915 -0.24806153 -0.0727247 0.7013117
## solar -0.35907757 -0.60285500 -0.28811402 0.6270784 0.1771782
## vwc -0.02202622 0.11336753 -0.91480721 -0.2376206 -0.3054944
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.9604336 1.4263716 1.1328953 0.3308608 0.1494387
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp 0.5528284 0.2927017 -0.5161444 0.58506534
## airtemp 0.5752002 0.1621790 -0.1780827 -0.78174760
## solar -0.2020948 0.9351353 0.2902520 -0.02081817
## vwc -0.5680521 0.1163857 -0.7858978 -0.21479254
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.7057470 1.0002871 0.1746091 0.1193568
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp -0.06063857 0.6678957 0.3108137 0.65002090 0.17636913
## airtemp -0.63144250 0.1589301 -0.1376075 0.04572657 -0.74497976
## windspeed 0.51761081 -0.3538444 -0.1743425 0.61575261 -0.44421461
## solar -0.27561807 -0.5266634 0.7914551 0.14140319 -0.01625548
## vwc -0.50370334 -0.3550724 -0.4771399 0.41980444 0.46508949
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.23911277 1.72684636 0.54766912 0.39193179 0.09443996
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.6870888 0.02383390 0.1457334 -0.71140898
## airtemp -0.6771075 -0.04481204 0.2262471 0.69880578
## solar -0.1690357 -0.73106034 -0.6610344 0.00335068
## vwc -0.2021419 0.68042237 -0.7004338 0.07454210
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.03648684 1.02746124 0.89866169 0.03739023
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp 0.3752825 0.59376864 -0.1039337 -0.41463060 0.56910553
## airtemp 0.4966456 -0.40594270 -0.2574321 0.55989800 0.45694269
## windspeed -0.4342070 0.48493444 0.2980482 0.63251122 0.29563442
## solar 0.4643719 -0.07556578 0.8795915 -0.00489644 -0.07030862
## vwc 0.4564310 0.49170899 -0.2457895 0.33838037 -0.61235550
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.4481220 1.6778419 0.5973827 0.2131039 0.0635494
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp 0.5665084 0.34854397 0.3268927 -0.6713616362
## airtemp 0.5966186 0.11575861 0.3279871 0.7232362109
## solar 0.4732758 0.05455024 -0.8792236 -0.0004228499
## vwc -0.3148403 0.92851565 -0.1119442 0.1618726593
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.54976689 0.85747377 0.55149723 0.04126211
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp 0.1450970 -0.71802429 0.09771796 -0.3415721 -0.5806614
## airtemp -0.6004558 -0.09764067 -0.23632691 0.6208544 -0.4342908
## windspeed 0.5147493 0.29416686 0.52145759 0.4530718 -0.4138923
## solar -0.4602201 -0.24019780 0.80022830 0.0306449 0.2986603
## vwc 0.3763380 -0.57504634 -0.14939021 0.5400510 0.4622975
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.1163562 1.5983508 0.6826193 0.3551998 0.2474739
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp 0.3514179 0.8152750 -0.1091725 -0.4471169
## airtemp 0.5591761 -0.1538098 -0.7403975 0.3398179
## solar -0.4926721 0.5451842 -0.1859677 0.6522763
## vwc 0.5666584 0.1201810 0.6366388 0.5090637
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.0705346 0.9766761 0.5071717 0.4456175
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp 0.01816347 0.8060002 -0.34196490 0.4114429 0.2526035
## airtemp 0.51226663 -0.3030941 -0.31018063 0.5893687 -0.4496103
## windspeed -0.42390614 -0.1006386 -0.80948875 -0.3019329 -0.2524677
## solar 0.56253028 -0.2173335 -0.35939066 -0.2955753 0.6479193
## vwc -0.49103590 -0.4484816 -0.04913558 0.5521151 0.5005024
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.1631494 1.2238474 0.8130995 0.5508904 0.2490133
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp 0.4779862 0.6158275 -0.55781490 -0.2848301
## airtemp 0.5150139 0.2064255 0.79477713 -0.2459238
## solar -0.4593670 0.7537318 0.22890821 0.4104527
## vwc 0.5433892 -0.1001668 -0.06908599 0.8306154
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.5500409 0.6705998 0.4340380 0.3453212
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.5189105 0.33778658 0.57383956 0.53604129
## airtemp -0.3690608 -0.91642990 0.13494875 0.07575777
## solar -0.4628322 0.09690332 -0.80668990 0.35446796
## vwc 0.6166948 -0.19148424 -0.04181384 0.76241259
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.8095797 0.8758223 0.8359594 0.4786386
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.6217691 0.1547735 -0.1977538 -0.741850206
## airtemp -0.4026852 0.7155895 0.4335509 0.371227483
## solar -0.3809989 -0.6288776 0.6777146 0.007466629
## vwc 0.5532519 0.2617055 0.5600268 -0.558383988
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.0239761 0.9737171 0.6990898 0.3032170
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp -0.5465201 -0.39589672 -0.24961662 0.2542445 -0.64624518
## airtemp -0.6159923 -0.06735731 -0.40166906 -0.3329823 0.58634568
## windspeed -0.2333193 0.68296847 0.03556769 -0.5302914 -0.44344351
## solar -0.2677192 -0.39731812 0.80177683 -0.3566891 0.01978789
## vwc 0.4424452 -0.46305633 -0.36365144 -0.6450167 -0.20379452
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.0013917 1.6419385 0.8650116 0.3239791 0.1676791
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.6150276 -0.05358385 0.36959315 0.6944571
## airtemp -0.5975550 -0.33517673 0.24557612 -0.6857675
## solar 0.4383070 0.04525365 0.89374763 -0.0839897
## vwc -0.2693624 0.93954108 0.06563873 -0.2009926
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.1967040 0.9423133 0.7199182 0.1410645
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.5567842 0.11989798 0.4458881 0.6905068
## airtemp -0.5281882 0.49115021 0.2236155 -0.6555798
## solar -0.4900021 0.06046533 -0.8569741 0.1477748
## vwc 0.4134085 0.86066247 -0.1295196 0.2675410
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.5875192 0.7239522 0.5066875 0.1818411
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp -0.1989475 -0.2286524 -0.9094261 0.08778720 -0.2708791887
## airtemp -0.4681377 -0.4644752 0.2860514 0.69518615 0.0008271347
## windspeed 0.5064199 -0.4897870 0.2066861 -0.07046171 -0.6752507565
## solar -0.4773710 -0.4957697 0.1270438 -0.70511109 0.1140508766
## vwc -0.5068756 0.4962882 0.1796092 -0.08284391 -0.6765002545
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.9205803 1.2681201 1.0019530 0.4625297 0.3468170
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.5386811 -0.4002899 0.3007319 -0.6776068
## airtemp -0.4623650 0.5753981 -0.6264574 -0.2503730
## solar -0.5204390 0.4252244 0.6027993 0.4300703
## vwc -0.4745362 -0.5725986 -0.3921025 0.5414812
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.3309064 1.0369961 0.3600766 0.2720209
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.5609533 -0.2941563 0.60560789 -0.4817080
## airtemp -0.5228684 -0.2763777 -0.78030326 -0.2033490
## solar -0.6222899 0.2771964 0.13111234 0.7202271
## vwc 0.1571476 -0.8719229 0.08470808 0.4559381
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.6758665 1.1210174 0.7161906 0.4869255
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp -0.4354317 -0.4163172 0.3965361 -0.6734151 -0.16232840
## airtemp -0.5637814 0.1631632 0.1173785 0.4862529 -0.63663866
## windspeed 0.2053099 0.7613473 0.5518309 -0.2586108 -0.08246944
## solar -0.5797996 0.1680646 0.1907211 0.2032556 0.74692658
## vwc -0.3379793 0.4383633 -0.6986340 -0.4493083 -0.06033388
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.3653586 1.0305111 0.8791172 0.5047098 0.2203033
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.4614902 -0.5742871 0.673015345 -0.06535574
## airtemp -0.5482611 0.3569852 0.002113468 0.75628492
## solar -0.5089968 0.5715871 0.076662593 -0.63900959
## vwc -0.4768215 -0.4648054 -0.735641710 -0.12421150
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.6496960 0.7024398 0.4485390 0.1993252
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.58944146 -0.04999413 0.42490763 0.68521009
## airtemp -0.59401370 0.05479135 0.34838432 -0.72303112
## solar -0.54573454 0.07338267 -0.83308604 0.05250178
## vwc 0.04336233 0.99454184 0.06363585 0.07040389
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.60256139 1.00437895 0.31593320 0.07712647
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp -0.5591419 0.28874097 0.37178847 0.2062021 -0.650571286
## airtemp 0.3029730 0.54878434 -0.55436584 0.5208792 -0.168542358
## windspeed 0.4146351 -0.34536141 0.51323535 0.6673601 -0.004816979
## solar -0.2106565 -0.70422144 -0.53315855 0.1737212 -0.381128470
## vwc 0.6158484 -0.01618827 0.08236069 -0.4589303 -0.634876650
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.9464354 1.1485110 0.9844688 0.6041041 0.3164807
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -7.003567e-01 0.000000e+00 -1.182970e-01 -7.039221e-01
## airtemp 7.046075e-01 -1.827958e-15 4.312355e-02 -7.082857e-01
## solar 3.468817e-15 1.000000e+00 -1.890567e-14 -2.740731e-16
## vwc -1.141437e-01 1.902567e-14 9.920414e-01 -5.315106e-02
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.8065737 1.0000000 0.9916133 0.2018130
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.6040650 -0.2817276 -0.28960990 -0.68692153
## airtemp 0.3693579 -0.6605420 0.58127338 -0.29896544
## solar -0.4936047 -0.5650829 0.01147165 0.66098729
## vwc 0.5050096 -0.4061961 -0.76033924 0.04306135
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.8443673 1.1896812 0.5753281 0.3906234
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp 0.5796390 0.3252849 0.3612202 0.11847675 -0.6431886
## airtemp 0.4953869 0.3419830 -0.2979127 -0.66337026 0.3298899
## windspeed -0.4268372 0.4533198 -0.5960252 -0.06729011 -0.5025307
## solar 0.4686875 -0.1460314 -0.6132261 0.60784980 0.1160999
## vwc -0.1294313 0.7418985 0.2224312 0.41460080 0.4598529
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.9618187 1.4333447 0.8522882 0.5838204 0.1687280
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp 5.861157e-01 0.000000e+00 5.395426e-01 6.044519e-01
## airtemp -6.390173e-01 3.183131e-14 -1.508243e-01 7.542605e-01
## solar -1.161750e-14 -1.000000e+00 -1.936405e-14 2.854972e-14
## vwc 4.981217e-01 1.747195e-14 -8.283392e-01 2.563765e-01
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.5043556 1.0000000 0.8517245 0.6439199
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.5807250 0.04605482 0.60199291 0.5461154
## airtemp -0.3998125 0.63619622 -0.62536026 0.2105442
## solar -0.6299652 -0.04890036 0.09398231 -0.7693633
## vwc 0.3256567 0.76859748 0.48754118 -0.2559478
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.0396645 1.0505106 0.6011260 0.3086988
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp 0.5995194 -0.1014413 -0.30088338 0.11360647 -0.72584356
## airtemp 0.5715270 0.1447190 -0.12583552 0.53891373 0.58834575
## windspeed 0.1812310 0.6761935 0.67824491 0.02671363 -0.22178363
## solar 0.1202214 -0.7058422 0.65101300 0.24987842 -0.03280955
## vwc 0.5163676 -0.1153919 0.09899763 -0.79593512 0.27701257
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.3397701 1.1355582 0.8646415 0.5422137 0.1178165
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp 7.001253e-01 0.000000e+00 -9.592469e-02 7.075472e-01
## airtemp 6.992378e-01 -8.552477e-16 -1.084817e-01 -7.066104e-01
## solar -2.839693e-16 -1.000000e+00 2.242686e-15 5.850396e-16
## vwc 1.445373e-01 2.172810e-15 9.894595e-01 -8.876652e-03
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.95198011 1.00000000 0.97976116 0.06825873
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp 0.6671646 -0.05565235 0.4201886 -0.6125649
## airtemp 0.7032124 0.01247316 -0.0862089 0.7056237
## solar 0.1521656 -0.70721007 -0.6547358 -0.2191361
## vwc 0.1929490 0.70469933 -0.6223590 -0.2807826
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.9022397 1.3770000 0.6111580 0.1096024
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp 0.61167796 0.1806687 -0.06768786 -0.3199880 0.69730547
## airtemp 0.56044906 -0.2772485 -0.21501729 0.7435784 -0.09944294
## windspeed 0.07601981 -0.7265541 -0.46256296 -0.4812371 -0.14417506
## solar 0.11960624 0.5998786 -0.68443815 -0.1151668 -0.37963269
## vwc 0.54005765 0.0525053 0.51649380 -0.3159847 -0.58221023
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.2252687 1.3531293 0.9620312 0.2725568 0.1870140
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp 6.348074e-01 0.000000e+00 -2.412320e-01 -7.340481e-01
## airtemp 4.749839e-01 8.621695e-16 8.711456e-01 1.244810e-01
## solar -9.422172e-18 1.000000e+00 -8.026463e-16 2.556273e-16
## vwc 6.094340e-01 -6.226122e-16 -4.276826e-01 6.675910e-01
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.26969641 1.00000000 0.64113326 0.08917033
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp 0.5930569 0.14245409 -0.3524528 -0.7097658
## airtemp 0.5900318 -0.09458769 -0.4203873 0.6827812
## solar 0.2951347 -0.85710641 0.4031014 -0.1255920
## vwc 0.4615642 0.48593066 0.7325031 0.1194533
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.1543021 0.9727592 0.5722700 0.3006687
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp -0.5930311 0.2975110 0.39805583 0.19413096 -0.6030473
## airtemp -0.1812661 0.7158527 0.09433525 0.16529356 0.6468975
## windspeed 0.4556559 0.4698802 0.25592764 -0.66200812 -0.2604545
## solar 0.1389000 -0.3559499 0.87554536 0.06824896 0.2876958
## vwc -0.6233307 -0.2270558 -0.02395374 -0.70148310 0.2593302
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.5972184 1.5121945 0.9726092 0.5629609 0.3550171
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp 0.7014437 0.07192502 -0.06097566 -0.70645987
## airtemp 0.3688362 0.46977510 0.72075142 0.35183609
## solar 0.0504967 -0.82735834 0.55337821 -0.08185855
## vwc -0.6077720 0.29935986 0.41300353 -0.60862550
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.8419939 1.0187001 0.9693508 0.1699552
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.4783405 -0.5160720 -0.5784912 -0.4125627
## airtemp -0.4552586 -0.5458404 0.5874160 0.3869630
## solar 0.5234778 -0.4774783 -0.4143003 0.5712625
## vwc -0.5384246 0.4557878 -0.3855459 0.5947359
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.6314007 1.2241833 0.6439513 0.5004647
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp 0.5326715 0.3724942 0.24319557 0.716901129 0.06646640
## airtemp 0.4022634 0.5496925 0.27291592 -0.679039789 0.02107754
## windspeed -0.5313796 0.4425721 0.04421117 0.083476936 0.71613031
## solar 0.2391576 0.2717610 -0.92963535 -0.009850206 0.06804933
## vwc 0.4635564 -0.5379240 0.01401087 -0.133762143 0.69113286
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.0620383 1.5524515 0.8867904 0.2843056 0.2144142
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp 0.6028000 -0.36695271 0.03082316 -0.7078332
## airtemp 0.4970788 0.08624102 0.75895442 0.4116593
## solar 0.3163096 0.90433718 -0.19709100 -0.2080328
## vwc -0.5380456 0.20020605 0.61983325 -0.5350058
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.1538998 0.9139624 0.7453918 0.1867460
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp -0.4497936 0.69091532 0.49551748 0.2734670
## airtemp -0.5179542 0.03757170 -0.74931525 0.4108995
## solar -0.4462694 -0.72182102 0.43630042 0.2990986
## vwc 0.5746762 0.01410016 0.05129344 0.8166501
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.2861665 0.7719674 0.5770300 0.3648361
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp 0.31665986 0.5867914 0.1988528066 0.70265535 0.14877978
## airtemp 0.22260855 0.6196357 0.2926293013 -0.69234960 -0.03895082
## windspeed -0.66515176 0.2335952 -0.0008019562 -0.04496092 0.70780243
## solar 0.08741772 0.3445149 -0.9323470025 -0.05559442 -0.03613760
## vwc 0.63253715 -0.3137996 -0.0745256807 -0.14769995 0.68851812
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.9888585 1.6636606 0.9042893 0.3862764 0.0569152
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp 0.6736359 -0.23359633 -0.04330536 -0.6998372
## airtemp 0.2703836 0.19042169 0.93344958 0.1389394
## solar 0.1753929 0.94914176 -0.22446797 -0.1340946
## vwc -0.6650900 0.09111667 0.27642479 -0.6877081
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.9548111 0.9883820 0.9399373 0.1168697
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4
## groundtemp 0.3916258 0.6269089 0.6404215 0.2085061
## airtemp 0.5223899 0.4429681 -0.6392142 -0.3497044
## solar 0.5670826 -0.3920196 -0.1905662 0.6988722
## vwc -0.5021506 0.5070356 -0.3807230 0.5880558
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.7911826 1.2575820 0.5173168 0.4339186
If you want to just make one plot (for testing):
# look at all_dates and pick a row to use (set to i)
i <- 13
cursite <- "TNHA"
month <- all_dates$months[i]
startdate <- all_dates$start[i]
enddate <- all_dates$end[i]
szn <<- all_dates$szn[i]
yr <<- substr(all_dates$start[i], 1, 4)
# big_df <- pick_site(site)
north_sensor <- "TNHA-SC"
south_sensor <- "TNHA-SA"
north_and_south <- df %>% filter(fullname == north_sensor | fullname == south_sensor)
big_df <<- df %>% filter(fullname == north_sensor | fullname == south_sensor) %>% filter(grounddepth == "30cm") %>% filter(vwcdepth == "30:45cm")
big_df <- big_df %>% select(Date, fullname, site, station, groundtemp, airtemp, windspeed, solar, vwc)
if(szn == "Winter"){
big_df <<- big_df %>% select(-solar)
}
# pca_df <- pick_dates(startdate, enddate, big_df)
pca_df <<- big_df %>% filter(Date >= startdate) %>% filter(Date < enddate)
# get rid of NAs
pca_df <<- na.omit(pca_df)
pca_df <<- unique(pca_df)
if(nrow(pca_df > 0)){
p <- calc_pca(pca_df)
pca <- p$pca
loads <- p$loads # not used at the moment
if(scree == T){
make_scree(pca)
}
if(eigen == T){
make_eigen(pca)
}
make_pca(pca_df, szn, yr, site)
}
## [1] "Eigenvectors (Loadings):"
## PC1 PC2 PC3 PC4 PC5
## groundtemp -0.5244021 -0.1240167 0.5394605 -0.09901801 0.63937477
## airtemp -0.5109256 -0.4146668 0.2344693 -0.08508085 -0.71048706
## windspeed 0.4004499 0.3898287 0.6928930 -0.38695990 -0.24048874
## solar -0.4246601 0.4733398 -0.3979478 -0.66084688 -0.02306821
## vwc 0.3510857 -0.6607982 -0.1246730 -0.62968256 0.16745467
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.82503025 1.24931010 0.66659405 0.24057303 0.01849257
#==================
## Code Graveyard
#==================
#The following code makes a bi-plot, which is similar to the code above, however it is not grouped at all. It is a bit harder to read, but it is very straightforward code for a simpler PCA.
# ### Bi-plots
# dev.new(height=7, width=7)
# biplot(scores[, 1:2], loadings[, 1:2], cex=0.7)
#NOTE: this is our code graveyard for code that can be used to find other PCA results, including k-means clustering. Above, we use clusters of TNHA-North and TNHA-South. Here, we use k-means clustering to group data points together and compare that to the different stations at TNHA. This can be useful, however we are particularly interested in identifying differences between north- and south-facing sensors.
# ### K Means Clustering
# pc_data <- summer_tnha_pca$x
#
# # Select the first two principal components
# pc_to_use <- pc_data[, 1:2]
# # Run k-means for different numbers of clusters
# wcss <- numeric()
# for (k in 1:10) {
# kmeans_result <- kmeans(pc_to_use, centers = k)
# wcss[k] <- kmeans_result$tot.withinss
# }
#
# # Plot the results
# plot(1:10, wcss, type = "b", xlab = "Number of Clusters", ylab = "Within-Cluster Sum of # Squares")
# # We can use 2-3 clusters (where the change in slope drops off)
# # Perform k-means clustering
# kmeans_result <- kmeans(pc_to_use, centers = 3) # Change 'centers' based on your specific # case
# # Add cluster assignments to the data
# clustered_data <- data.frame(pc_to_use, cluster = kmeans_result$cluster)
#
# # Plot the clusters
# ggplot(clustered_data, aes(x = PC1, y = PC2, color = factor(cluster))) +
# geom_point() +
# theme_minimal() +
# labs(title = "Clusters in Principal Component Space")
Find Each Data Point
# #Want to look at clustered data
#
# cluster_sensors = data.frame(clustered_data, summer_tnha$station)
#
# cluster1 = filter(cluster_sensors, cluster == 1)
# cluster2 = filter(cluster_sensors, cluster == 2)
# cluster3 = filter(cluster_sensors, cluster == 3)
#
# cluster_sensors$cluster = as.factor(cluster_sensors$cluster)
#
# ggplot(cluster_sensors, aes(x = summer_tnha.station, fill = cluster)) +
# geom_bar(position = "dodge") +
# theme_minimal() +
# labs(title = "Number of Instances in Each Group",
# x = "Category",
# y = "Count")
#
# p = ggplot(cluster_sensors, aes(x = PC1, y = PC2)) +
# geom_point(aes(color = summer_tnha.station, shape = cluster), size = 2) +
# scale_shape_manual(values = c(16, 17, 18)) + # Set shapes manually
# theme_minimal() +
# labs(title = "Biplot of Eigenvectors and PC Clusters",
# x = "PC1",
# y = "PC2")
# p + geom_segment(data = as.data.frame(eigenvectors), aes(x = 0, y = 0, xend = PC1*4, yend = # PC2*4),
# arrow = arrow(type = "closed", length = unit(0.1, "inches")),
# linewidth = 0.5, color = "black") +
# geom_text(data = as.data.frame(eigenvectors), aes(x = PC1*2.5, y = PC2*5+0.1, label = # rownames(eigenvectors)))
#
#